require('./registered.less')
//公共样式
require('../../assets/css/normalize.css')
//axios
import axios from 'axios'
//获取元素
function $(arr) {
   return document.querySelector(arr)
}

const CaptchaMini = require('captcha-mini')

let captcha1 = new CaptchaMini({
   length: 4,
   fontSize: 80,
});
let captcha = ''
captcha1.draw(document.querySelector('#img'), r => {
   captcha = r
   

});
// console.log(captcha1.r);

let reg1 = /^1[3|4|5|6|7|8|9][0-9]{9}$/;  //正则表达式 账号
let reg2 = /^[0-9]{6}$/;// 正则表达式  密码
window.onload=()=>{
   $('.btn').addEventListener('click', function () {
      if (!reg1.test($('.empn').value) ){
         $('.regs').style.opacity = 0.8
         $('.regs_').innerHTML = '手机号输入错误' 
         $('.regs_1').innerHTML = '×'
      }else if($('.verification').value.toLowerCase() !== captcha.toLowerCase()){
         $('.regs').style.opacity = 0.8
         $('.regs_').innerHTML = '验证码输入错误' 
         $('.regs_1').innerHTML = '×'
      }else if(!reg2.test($('.password').value)){
         $('.regs').style.opacity = 0.8
         $('.regs_').innerHTML = '密码格式错误' 
         $('.regs_1').innerHTML = '×'
      }else if(!reg2.test($('.password_').value)){
         $('.regs').style.opacity = 0.8
         $('.regs_').innerHTML = '密码格式错误' 
         $('.regs_1').innerHTML = '×'
      }
            else{  //正则表达式验证成功过后执行         检测账号是否存在的请求
               axios.post('http://fitness.h5.itsource.cn:3701/api/user/isExist',{
                  account:$('.empn').value,
               }).then(function(res){
                  if(res.data.errno === 10002){
                     $('.regs').style.opacity = 0.8
                     $('.regs_').innerHTML = '账号已存在,请登录' 
                     $('.regs_1').innerHTML = '×'
                     setInterval(function () {
                        location.href = './login_.html'
                     }, 2000)
                  }
               })
               //账号注册的请求
               axios.post('http://fitness.h5.itsource.cn:3701/api/user/register', {
                  account: $('.empn').value,
                  password: $('.password_').value,
               }).then(function (res) {   // 当后端验证成功之后
                  if (res.data.errno === 0) {  //执行当errno===0的时候跳转,未注册过
                     $('.regs').style.opacity = 0.8//显示
                     $('.regs_').innerHTML = '注册成功'  //文字跟换为注册成功，两秒后跳转登录页面
                     $('.regs_1').innerHTML = '√'
                     setInterval(function () {
                        location.href = './login_.html'
                     }, 2000) //成功跳转登录
                  } else if (res.data.errno === 10002) {//已注册=10002的时候显示蒙层
                     $('.regs').style.opacity = 0.8
                  }
               })
      }
   })
   //点击蒙层隐藏自身
   $('.regs').addEventListener('click', () => {
      $('.regs').style.opacity = 0
   
   })
   
}






// //跳转
// paws.addEventListener('click',function(){
//    location.href='../login_/login_.html'
// })




